﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace UDS.Components
{
    /// <summary>
    /// Task 
    /// </summary>
    public class Task
    {
        #region 添加任务

        /// <summary>
        /// 任务添加
        /// </summary>
        /// <param name="tc">Task类</param>
        public string AddTask(TaskClass tc)
        {
            // create data object and params
            var data = new Database();
            string TaskID;
            var parameters = new[]
            {
                Database.MakeInParam("@ArrangedBy", SqlDbType.NVarChar, 20, tc.ArrangedBy),
                Database.MakeInParam("@Subject", SqlDbType.NVarChar, 50, tc.Subject),
                Database.MakeInParam("@Detail", SqlDbType.NVarChar, 300, tc.Detail),
                Database.MakeInParam("@ProjectID", SqlDbType.SmallInt, 20, tc.ProjectID),
                Database.MakeInParam("@StartTime", SqlDbType.DateTime, 20, DateTime.Parse(tc.StartTime)),
                Database.MakeInParam("@EndTime", SqlDbType.DateTime, 20, DateTime.Parse(tc.EndTime)),
                Database.MakeInParam("@Attribute", SqlDbType.Int, 1, tc.Attribute),
                Database.MakeInParam("@Type", SqlDbType.Int, 1, tc.Type),
                Database.MakeInParam("@Status", SqlDbType.Bit, 1, tc.Status),
                Database.MakeInParam("@Tag", SqlDbType.Int, 1, tc.Tag),
                Database.MakeInParam("@IsAwake", SqlDbType.Bit, 1, tc.IsAwake),
                Database.MakeInParam("@AwakeTime", SqlDbType.DateTime, 20, DateTime.Parse(tc.AwakeTime)),
                Database.MakeInParam("@ContractList", SqlDbType.NVarChar, 6000, tc.ContractList),
                Database.MakeInParam("@CooperatorList", SqlDbType.NVarChar, 6000, tc.CooperatorList),
                Database.MakeOutParam("@InsertedTaskID", SqlDbType.Int, 20)
            };

            try
            {
                data.RunProc("dbo.sproc_AddTask", parameters);
                TaskID = parameters[14].Value.ToString();
                if (TaskID == string.Empty)
                {
                    return null;
                }
                return TaskID;
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("任务添加出错!", ex);
            }
        }

        #endregion

        #region 添加任务评论

        /// <summary>
        /// 添加任务评论
        /// </summary>
        public bool AddTaskComment(string Username, string Comment, int TaskID)
        {
            // create data object and params
            var data = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@Username", SqlDbType.NVarChar, 200, Username),
                Database.MakeInParam("@Comment", SqlDbType.NVarChar, 4000, Comment),
                Database.MakeInParam("@TaskID", SqlDbType.Int, 6, TaskID)
            };

            try
            {
                data.RunProc("dbo.sproc_Task_AddComment", parameters);
                return true;
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("任务添加出错!", ex);
            }
        }

        #endregion

        #region 删除任务评论

        /// <summary>
        /// 删除任务评论
        /// </summary>
        public bool DeleteTaskComment(int ID)
        {
            // create data object and params
            var data = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@ID", SqlDbType.Int, 8, ID)
            };

            try
            {
                data.RunProc("dbo.sproc_Task_DeleteComment", parameters);
                return true;
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("删除任务评论出错!", ex);
            }
        }

        #endregion

        #region 添加任务订阅

        /// <summary>
        /// 添加任务订阅
        /// </summary>
        public bool AddTaskSubscription(string Username, string ExUsername, int TaskID, string Date)
        {
            // create data object and params
            var data = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@Username", SqlDbType.NVarChar, 200, Username),
                Database.MakeInParam("@ExUsername", SqlDbType.NVarChar, 200, ExUsername),
                Database.MakeInParam("@TaskID", SqlDbType.Int, 6, TaskID),
                Database.MakeInParam("@Date", SqlDbType.NVarChar, 30, Date)
            };

            try
            {
                data.RunProc("dbo.sproc_Task_AddSubscription", parameters);
                return true;
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("添加任务订阅出错!", ex);
            }
        }

        #endregion

        #region 删除任务订阅

        /// <summary>
        /// 删除任务订阅
        /// </summary>
        public bool DeleteTaskSubscription(string Username, string TaskID)
        {
            // 定义数据库操作类及DataReader
            var data = new Database();
            // 执行存储过程，并返回SqlDataReader对象
            var parameters = new[]
            {
                Database.MakeInParam("@Username", SqlDbType.NVarChar, 200, Username),
                Database.MakeInParam("@TaskID", SqlDbType.NVarChar, 200, TaskID)
            };

            try
            {
                data.RunProc("dbo.sproc_Task_DeleteSubscription", parameters);
                return true;
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("删除某任务订阅出错!", ex);
            }
            finally
            {
                data.Dispose();
            }
        }

        #endregion

        #region 添加任务至日程表

        /// <summary>
        /// 添加任务至日程表
        /// </summary>
        public bool AddTaskToSchedule(int TaskID, int PeriodID, string Username, string Date, bool IsConfirm)
        {
            var data = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@TaskID", SqlDbType.Int, 8, TaskID),
                Database.MakeInParam("@PeriodID", SqlDbType.Int, 8, PeriodID),
                Database.MakeInParam("@Username", SqlDbType.NVarChar, 20, Username),
                Database.MakeInParam("@Date", SqlDbType.NVarChar, 30, Date),
                Database.MakeInParam("@IsConfirm", SqlDbType.Bit, 1, IsConfirm)
            };

            try
            {
                data.RunProc("dbo.sproc_AddTaskToSchedule", parameters);
                return true;
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("任务添加至日程表出错!", ex);
            }
        }

        #endregion

        #region 读取工作时间段信息 SqlDataReader

        /// <summary>
        /// 返回项目相关信息
        /// </summary>
        public SqlDataReader GetPeriodInfo()
        {
            // 定义数据库操作类及DataReader
            var data = new Database();

            // 执行存储过程，并返回SqlDataReader对象
            try
            {
                return data.RunDataReader("dbo.sproc_GetPeriodInfo");
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("工作时间段信息读取出错!", ex);
            }
        }

        #endregion

        #region 检测日程是否冲突

        /// <summary>
        ///  检测日程是否冲突
        /// </summary>
        public bool CheckExist(int PeriodID, string Username, string Date)
        {
            var data = new Database();
            bool IsExist;
            var parameters = new[]
            {
                Database.MakeInParam("@PeriodID", SqlDbType.Int, 8, PeriodID),
                Database.MakeInParam("@Username", SqlDbType.NVarChar, 20, Username),
                Database.MakeInParam("@Date", SqlDbType.DateTime, 30, DateTime.Parse(Date)),
                Database.MakeOutParam("@IsExist", SqlDbType.Bit, 1)
            };
            try
            {
                data.RunProc("dbo.sproc_ScheduleCheckExist", parameters);
                IsExist = parameters[3].Value.ToString() == "True" ? true : false;
                return IsExist;
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("检测冲突出错!", ex);
            }
        }

        #endregion

        #region 获得某用户某日的任务

        /// <summary>
        /// 获得某用户某日的任务
        /// </summary>
        public SqlDataReader GetDayTaskInSchedule(string Date, string Username)
        {
            // 定义数据库操作类及DataReader
            var data = new Database();

            // 执行存储过程，并返回SqlDataReader对象
            var parameters = new[]
            {
                Database.MakeInParam("@Username", SqlDbType.NVarChar, 20, Username),
                Database.MakeInParam("@Date", SqlDbType.NVarChar, 30, Date)
            };

            try
            {
                return data.RunDataReader("dbo.sproc_GetDayTaskInSchedule", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("获得某用户某日的任务出错!", ex);
            }
        }

        #endregion

        #region 获得某任务的状态

        /// <summary>
        /// 获得某任务的状态
        /// </summary>
        public SqlDataReader GetTaskStatus(int TaskID)
        {
            // 定义数据库操作类及DataReader
            var data = new Database();

            // 执行存储过程，并返回SqlDataReader对象
            var parameters = new[]
            {
                Database.MakeInParam("@TaskID", SqlDbType.Int, 20, TaskID)
            };

            try
            {
                return data.RunDataReader("dbo.sproc_GetTaskStatus", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("获得某任务的状态出错!", ex);
            }
        }

        #endregion

        #region 获得某用户某任务的状态

        /// <summary>
        /// 获得某任务的状态
        /// </summary>
        public int GetTaskStatusBySomeone(int TaskID, string Username)
        {
            // 定义数据库操作类及DataReader
            var data = new Database();

            // 执行存储过程，并返回SqlDataReader对象
            var parameters = new[]
            {
                Database.MakeInParam("@TaskID", SqlDbType.Int, 20, TaskID),
                Database.MakeInParam("@Username", SqlDbType.NVarChar, 40, Username)
            };

            try
            {
                using (SqlDataReader dataReader = data.RunDataReader("dbo.sproc_GetTaskStatusBySomeone", parameters))
                {
                    if (dataReader.Read())
                    {
                        return Int32.Parse(dataReader[0].ToString());
                    }
                    return -1;
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("获得某任务的状态出错!", ex);
            }
        }

        #endregion

        #region 获得任务的评论列表

        /// <summary>
        /// 获得任务的评论列表
        /// </summary>
        public SqlDataReader GetTaskComment(int TaskID)
        {
            // 定义数据库操作类及DataReader
            var data = new Database();

            // 执行存储过程，并返回SqlDataReader对象
            var parameters = new[]
            {
                Database.MakeInParam("@TaskID", SqlDbType.Int, 20, TaskID)
            };

            try
            {
                return data.RunDataReader("dbo.sproc_GetTaskComment", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("获得任务的评论列表出错!", ex);
            }
        }

        #endregion

        #region 处理任务

        /// <summary>
        /// 接受日程
        /// </summary>
        public SqlDataReader DealTask(string IDS, int opt, string username, string date)
        {
            // 定义数据库操作类及DataReader
            var data = new Database();

            // 执行存储过程，并返回SqlDataReader对象
            var parameters = new[]
            {
                Database.MakeInParam("@IDS", SqlDbType.NVarChar, 2000, IDS),
                Database.MakeInParam("@opt", SqlDbType.Int, 1, opt),
                Database.MakeInParam("@username", SqlDbType.NVarChar, 30, username)
                //	   Database.MakeInParam("@date",  SqlDbType.NVarChar, 40, date)
            };

            try
            {
                return data.RunDataReader("dbo.sproc_DealTask", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("处理日程出错!", ex);
            }
        }

        #endregion

        #region 获得某用户某日的详细任务

        /// <summary>
        /// 获得某用户某日的任务
        /// </summary>
        public SqlDataReader GetTodayTaskBySomeone(string Date, string Username, int Type)
        {
            // 定义数据库操作类及DataReader
            var data = new Database();

            // 执行存储过程，并返回SqlDataReader对象
            var parameters = new[]
            {
                Database.MakeInParam("@Username", SqlDbType.NVarChar, 20, Username),
                Database.MakeInParam("@Date", SqlDbType.NVarChar, 30, Date),
                Database.MakeInParam("@Type", SqlDbType.Int, 3, Type)
            };

            try
            {
                return data.RunDataReader("dbo.sproc_GetTodayTaskBySomeone", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("获得某用户某日的任务出错!", ex);
            }
        }

        #endregion

        #region 获得某用户所有的任务列务

        /// <summary>
        /// 获得某用户某日的任务
        /// </summary>
        public SqlDataReader GetAllTaskBySomeone(string Date, string Username, int Type)
        {
            // 定义数据库操作类及DataReader
            var data = new Database();

            // 执行存储过程，并返回SqlDataReader对象
            var parameters = new[]
            {
                Database.MakeInParam("@Username", SqlDbType.NVarChar, 20, Username),
                Database.MakeInParam("@Date", SqlDbType.NVarChar, 30, Date),
                Database.MakeInParam("@Type", SqlDbType.Int, 3, Type)
            };

            try
            {
                return data.RunDataReader("dbo.sproc_GetAllTaskBySomeone", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("获得某用户所有的任务出错!", ex);
            }
        }

        #endregion

        #region 获得某用户某日的前5条任务 返回数组记录ID

        /// <summary>
        /// 获得某用户某日的前5条任务
        /// </summary>
        public string[] GetTop5DayTaskList(string Date, string Username)
        {
            // 定义数据库操作类及DataReader
            var data = new Database();

            var a = new[] { "0", "0", "0", "0", "0" };
            // 执行存储过程，并返回SqlDataReader对象
            var parameters = new[]
            {
                Database.MakeInParam("@Username", SqlDbType.NVarChar, 20, Username),
                Database.MakeInParam("@Date", SqlDbType.NVarChar, 30, Date)
            };

            try
            {
                using (SqlDataReader dataReader = data.RunDataReader("dbo.sproc_GetTop5DayTaskList", parameters))
                {
                    int c = 0;
                    while (dataReader.Read())
                    {
                        a[c] = dataReader[0].ToString();
                        c++;
                    }
                }
                return a;
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("获得某用户某日的前5条任务出错!", ex);
            }
        }

        #endregion

        #region 获得前5条已经全部确认的任务列表

        /// <summary>
        /// 获得前5条已经全部确认的任务列表
        /// </summary>
        public string[] GetTop5ConfirmedTaskList(string Date, string Username)
        {
            // 定义数据库操作类及DataReader
            var data = new Database();
            var a = new[] { "0", "0", "0", "0", "0" };
            // 执行存储过程，并返回SqlDataReader对象
            var parameters = new[]
            {
                Database.MakeInParam("@Username", SqlDbType.NVarChar, 20, Username),
                Database.MakeInParam("@Date", SqlDbType.NVarChar, 30, Date)
            };

            try
            {
                using (SqlDataReader dataReader = data.RunDataReader("dbo.sproc_GetTop5ConfirmedTask", parameters))
                {
                    int c = 0;
                    while (dataReader.Read())
                    {
                        if (dataReader[1].ToString() == dataReader[2].ToString())
                        {
                            a[c] = dataReader[0].ToString();
                        }
                        c++;
                    }
                }
                return a;
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("获得前5条已经全部确认的任务列表出错!", ex);
            }
        }

        #endregion

        #region 获得某条任务某天的的时间段

        /// <summary>
        /// 获得某条任务某天的的时间段 返回长度为11的int型数组，如果数组的某个原素没有值，则值为0
        /// </summary>
        public int[] GetTaskPeriod(string Date, string Username, int TaskID)
        {
            // 定义数据库操作类及DataReader
            var data = new Database();
            var a = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            // 执行存储过程，并返回SqlDataReader对象
            var parameters = new[]
            {
                Database.MakeInParam("@Username", SqlDbType.NVarChar, 20, Username),
                Database.MakeInParam("@Date", SqlDbType.NVarChar, 30, Date),
                Database.MakeInParam("@TaskID", SqlDbType.Int, 8, TaskID)
            };

            try
            {
                using (SqlDataReader dataReader = data.RunDataReader("dbo.sproc_GetTaskPeriod", parameters))
                {
                    while (dataReader.Read())
                    {
                        a[Int32.Parse(dataReader[0].ToString()) - 1] = 7 + Int32.Parse(dataReader[0].ToString());
                    }
                }
                return a;
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("获得某条任务某天的的时间段出错!", ex);
            }
        }

        #endregion

        #region 获得某条任务的详细信息

        /// <summary>
        /// 获得某条任务的详细信息
        /// </summary>
        public TaskClass GetTaskDetail(int TaskID)
        {
            // 定义数据库操作类及DataReader
            var data = new Database();
            var tsk = new TaskClass();
            // 执行存储过程，并返回SqlDataReader对象
            var parameters = new[]
            {
                Database.MakeInParam("@TaskID", SqlDbType.Int, 8, TaskID)
            };

            try
            {
                using (SqlDataReader dataReader = data.RunDataReader("dbo.sproc_GetTaskDetail", parameters))
                {
                    if (dataReader.Read())
                    {
                        tsk.ArrangedBy = dataReader["ArrangedBy"].ToString();
                        tsk.Detail = dataReader["Detail"].ToString();
                        tsk.StartTime = dataReader["StartTime"].ToString();
                        tsk.EndTime = dataReader["EndTime"].ToString();
                        tsk.ProjectID = Int32.Parse(dataReader["ProjectID"].ToString());
                        tsk.Subject = dataReader["Subject"].ToString();
                        tsk.CooperatorList = dataReader["CooperatorList"].ToString();
                        tsk.Type = Int32.Parse(dataReader["Type"].ToString());
                        tsk.Attribute = dataReader["Attribute"].ToString() == "True" ? 1 : 0;
                        tsk.ContractList = dataReader["ContractList"].ToString();
                    }
                }

                return tsk;
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("获得某条任务某天的的时间段出错!", ex);
            }
        }

        #endregion

        #region 删除某任务

        /// <summary>
        /// 删除某任务
        /// </summary>
        public void DeleteTask(int TaskID)
        {
            // 定义数据库操作类及DataReader
            var data = new Database();
            // 执行存储过程，并返回SqlDataReader对象
            var parameters = new[]
            {
                Database.MakeInParam("@TaskID", SqlDbType.Int, 8, TaskID)
            };

            try
            {
                data.RunProc("dbo.sproc_DeleteTask", parameters);
                data.Dispose();
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("删除某任务!", ex);
            }
            finally
            {
                data.Dispose();
            }
        }

        #endregion
    }
}